package com.liu.qqRobot.q.handler;

import com.liu.common.content.RedisKeyInterface;
import com.liu.common.utils.QQClientUtils;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class LoginHandler {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        Boolean hasKey = redisTemplate.hasKey(RedisKeyInterface.USER_LOGIN_WEB_REDIS_KEY + groupId + "" + userId);
        if (Boolean.TRUE.equals(hasKey)) {
            String loginStatus = redisTemplate.opsForValue().get(RedisKeyInterface.USER_LOGIN_WEB_REDIS_KEY + groupId + "" + userId);
            if (Objects.equals(loginStatus, "1")) {
                QQClientUtils.sendMsg("请勿重复登录", userId, groupId, isGroup);
            } else {
                redisTemplate.opsForValue().set(RedisKeyInterface.USER_LOGIN_WEB_REDIS_KEY + groupId + "" + userId, "1", 1, TimeUnit.DAYS);
                QQClientUtils.sendMsg("登录成功有效期一天", userId, groupId, isGroup);
            }
        } else {
            QQClientUtils.sendMsg("请先在网页warframeBot输入登录信息", userId, groupId, isGroup);
        }
    }
}
